﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using AppToolKit.Core.Win32;


namespace CenterDateAchieve
{
    [RunInstaller(true)]
    public partial class ServiceInstaller : Installer
    {
        public ServiceInstaller()
        {
            InitializeComponent();
        }
        // Methods
        public static void InstallService(string svcPath, string svcName, string svcDispName)
        {
            try
            {
                IntPtr sc_handle = WindowServiceApi.OpenSCManager(null, null, 2);
                if (sc_handle.ToInt32() == 0)
                {
                    throw new ApplicationException(string.Format("打开SCManager失败,错误{0}", sc_handle.ToInt32()));
                }
                IntPtr sv_handle = WindowServiceApi.CreateService(sc_handle, svcName, svcDispName, 0xf01ff, 0x10, 2, 1, svcPath, null, 0, null, null, null);
                if (sv_handle.ToInt32() == 0)
                {
                    WindowServiceApi.CloseServiceHandle(sc_handle);
                    throw new ApplicationException(string.Format("创建服务失败,错误{0}", sv_handle.ToInt32()));
                }
                int i = WindowServiceApi.StartService(sv_handle, 0, null);
                WindowServiceApi.CloseServiceHandle(sc_handle);
                if (i == 0)
                {
                    throw new ApplicationException(string.Format("启动服务失败,错误{0}", sv_handle.ToInt32()));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void UnInstallService(string svcName)
        {
            IntPtr sc_handle = WindowServiceApi.OpenSCManager(null, null, 1);
            if (sc_handle.ToInt32() == 0)
            {
                throw new ApplicationException(string.Format("打开SCManager失败,错误{0}", sc_handle.ToInt32()));
            }
            IntPtr svc_handle = WindowServiceApi.OpenService(sc_handle, svcName, 0xf01ff);
            if (svc_handle.ToInt32() != 0)
            {
                WindowServiceApi.SERVICE_STATUS serviceStatus = new WindowServiceApi.SERVICE_STATUS();
                if (WindowServiceApi.QueryServiceStatus(svc_handle, ref serviceStatus) && (serviceStatus.dwCurrentState == WindowServiceApi.SERVICE_STATE.SERVICE_RUNNING))
                {
                    WindowServiceApi.ControlService(svc_handle, WindowServiceApi.SERVICE_CONTROL.STOP, ref serviceStatus);
                }
                if (WindowServiceApi.DeleteService(svc_handle) != 0)
                {
                    WindowServiceApi.CloseServiceHandle(sc_handle);
                    return;
                }
                WindowServiceApi.CloseServiceHandle(sc_handle);
                throw new ApplicationException(string.Format("删除服务失败,错误{0}", svc_handle.ToInt32()));
            }
            WindowServiceApi.CloseServiceHandle(sc_handle);
            throw new ApplicationException(string.Format("打开服务失败,错误{0}", svc_handle.ToInt32()));
        }

    }
}
